package com.zebraphp.devtool.httpUtils.NetOkHttp.Interceptor;

import com.zebraphp.devtool.util.ILog;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/*
 * 请求日志，拦截器
 */

public class LoggerInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();

        long t1 = System.nanoTime();
        ILog.tagGeneral(String.format("Sending request %s on %s%n%s",request.url(), chain.connection(), request.headers()));

        //官方文档中，少了下面的代码
        Request requestq = new Request.Builder().url( "https://publicobject.com/helloworld.txt").build();

        Response response = chain.proceed(requestq);

        long t2 = System.nanoTime();
        ILog.tagGeneral(String.format("Received response for %s in %.1fms%n%s", response.request().url(), (t2 - t1) / 1e6d, response.headers()));

        return response;
    }
}
